package com.lengxf.pay.starter.parent.pay;

import com.lengxf.pay.common.bean.*;
import com.lengxf.pay.common.service.BasePayService;
import com.lengxf.pay.starter.parent.bean.PayBackResult;
import com.lengxf.pay.starter.parent.bean.QueryResult;
import com.lengxf.pay.starter.parent.bean.RefundQueryResult;
import com.lengxf.pay.starter.parent.bean.RefundResult;
import com.lengxf.pay.starter.parent.transfer.TransferService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@AllArgsConstructor
public class WebBasePayService {

    private final TransferService transferService;

    private final PayServiceFinder payServiceFinder;


    public String toPay(String tenantryId, PayOrder payOrder) {
        BasePayService basePayService = payServiceFinder.find(tenantryId);
        return basePayService.toPay(payOrder);
    }

    public RefundResult refund(RefundOrder refundOrder, String tenantryId) {
        BasePayService basePayService = payServiceFinder.find(tenantryId);
        Map<String, Object> result = basePayService.refund(refundOrder);
        return transferService.get(basePayService).refundResultTransfer(result);
    }

    public QueryResult query(QueryOrder queryOrder, String tenantryId) {
        BasePayService basePayService = payServiceFinder.find(tenantryId);
        Map<String, Object> result = basePayService.query(queryOrder);
        return transferService.get(basePayService).queryResultTransfer(result);
    }

    public RefundQueryResult refundQuery(RefundOrderQuery refundOrderQuery, String tenantryId) {
        BasePayService basePayService = payServiceFinder.find(tenantryId);
        Map<String, Object> result = basePayService.refundQuery(refundOrderQuery);
        return transferService.get(basePayService).refundQueryResultTransfer(result);
    }

    public PayBackResult payBack(HttpServletRequest request, String tenantryId) {
        CallBackParam callBackParam = new PayBackParamConverter(request).convert();
        BasePayService basePayService = payServiceFinder.find(tenantryId);
        Map<String, Object> result = basePayService.payBack(callBackParam);
        return transferService.get(basePayService).payBackResultTransfer(result);
    }

}
